package com.ruin.tech.ahri.controller;

import com.ruin.tech.ahri.result.Result;
import com.ruin.tech.ahri.result.ResultCode;
import com.ruin.tech.ahri.result.ResultFactory;
import com.ruin.tech.ahri.service.IUploadService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @program ahri
 * @description: 上传文件控制器
 * @author: ruin
 * @create: 2019/11/28 10:52
 */

@Slf4j
@RestController
@RequestMapping(value = "/upload")
public class UploadController {

    @Autowired
    IUploadService uploadService;

    @PostMapping(value = "/avatar", produces = "application/json;charset=utf-8")
    public Result uploadAvatar(@RequestParam("avatar") MultipartFile file, HttpServletRequest request) {
//        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
        if(!file.isEmpty()){
            String avatarUrl = null;
            Long userId = Long.valueOf(request.getParameter("id"));
            if(userId == null || userId == 0){
                log.error("首页视频上传保存失败："+file.getOriginalFilename());
                return ResultFactory.buildFailResult(ResultCode.NO_LOGIN_EXCEPTION);
            }
            try{
                avatarUrl = uploadService.saveAvatar(file,userId);
                return ResultFactory.buildSuccessResult(avatarUrl);
            }catch (Exception e){
                log.error("头像上传保存失败："+file.getOriginalFilename(),e);
                return ResultFactory.buildFailResult(ResultCode.UPLOAD_AVATAR_FAIL);
            }
        }else{
            return ResultFactory.buildFailResult(ResultCode.UPLOAD_AVATAR_FAIL);
        }

    }

    @PostMapping(value = "/video", produces = "application/json;charset=utf-8")
    public Result uploadVideo(@RequestParam("video") MultipartFile file, HttpServletRequest request) {
//        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
        if(!file.isEmpty()){
            String videoUrl = null;
            Long userId = Long.valueOf(request.getParameter("id"));
            if(userId == null || userId == 0){
                log.error("首页视频上传保存失败："+file.getOriginalFilename());
                return ResultFactory.buildFailResult(ResultCode.NO_LOGIN_EXCEPTION);
            }
            try{
                videoUrl = uploadService.saveVideo(file);
                return ResultFactory.buildSuccessResult(videoUrl);
            }catch (Exception e){
                log.error("首页视频上传保存失败："+file.getOriginalFilename(),e);
                return ResultFactory.buildFailResult(ResultCode.UPLOAD_VIDEO_FAIL);
            }
        }else{
            return ResultFactory.buildFailResult(ResultCode.UPLOAD_VIDEO_FAIL);
        }

    }

    @PostMapping(value = "/article/img", produces = "application/json;charset=utf-8")
    public Result uploadArticleImg(@RequestParam("articleImg") MultipartFile file, HttpServletRequest request) {
//        String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/";
        if(!file.isEmpty()){
            String articleImg = null;
            Long userId = Long.valueOf(request.getParameter("id"));
            if(userId == null || userId == 0){
                log.error("未登录无法,文章图片上传失败："+file.getOriginalFilename());
                return ResultFactory.buildFailResult(ResultCode.NO_LOGIN_EXCEPTION);
            }
            try{
                articleImg = uploadService.saveArticleImg(file);
                return ResultFactory.buildSuccessResult(articleImg);
            }catch (Exception e){
                log.error("文章图片上传失败："+file.getOriginalFilename(),e);
                return ResultFactory.buildFailResult(ResultCode.UPLOAD_ARTICLE_IMG_FAIL);
            }
        }else{
            return ResultFactory.buildFailResult(ResultCode.UPLOAD_ARTICLE_IMG_FAIL);
        }

    }
}
